package com.scsj.supermarket.view.activity.goodmodel;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.GoodsCommDetailsAdapter;
import com.scsj.supermarket.view.activity.baseactivitymodel.BaseActivity;

import java.util.ArrayList;
import java.util.List;


/**
 * 商品评价详情
 */
public class GoodCommDetailActivity extends BaseActivity implements View.OnClickListener {
    public static final String TAG = "商品评价详情";

    private RecyclerView recycler_view;
    private GoodsCommDetailsAdapter goodsCommDetailsAdapter;

    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_good_comm_detail);

        initToolbar();
        setToolbarTitle("评价详情");
        hasBack(true);
    }

    @Override
    protected void initView() {
        recycler_view = findViewById(R.id.recycler_view);
        recycler_view.setLayoutManager(new LinearLayoutManager(this));


    }

    @Override
    protected void initListener() {

    }


    @Override
    protected void initData() {
        List<String> strings = new ArrayList<>();
        strings.add("酒是作为礼物送人的，就不做评价了");
        strings.add("物流超级快，确实值得称赞，啦啦啦啦，真是物美价廉。物流超级快，确实值得称赞，啦啦啦啦，真是物美价廉。");
        goodsCommDetailsAdapter = new GoodsCommDetailsAdapter(strings);
        recycler_view.setAdapter(goodsCommDetailsAdapter);

        View headerView = getLayoutInflater().inflate(R.layout.activity_good_comm_detail_header, null);
        headerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        LinearLayout photo_ll = headerView.findViewById(R.id.photo_ll);
        List<String> imgs = getGoodsEvaluateList(5);
        for (int i = 0; i < imgs.size(); i++) {
            View photoView = LayoutInflater.from(this).inflate(R.layout.list_item_tiezi_detail_image, null);
            final ImageView iv = photoView.findViewById(R.id.iv);
            String img = imgs.get(i);
            Glide.with(this)
                    .load(img)
                    .listener(new RequestListener<String, GlideDrawable>() {
                        @Override
                        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                            if (iv == null) {
                                return false;
                            }
                            if (iv.getScaleType() != ImageView.ScaleType.FIT_XY) {
                                iv.setScaleType(ImageView.ScaleType.FIT_XY);
                            }
                            ViewGroup.LayoutParams params = iv.getLayoutParams();
                            int vw = iv.getWidth() - iv.getPaddingLeft() - iv.getPaddingRight();
                            float scale = (float) vw / (float) resource.getIntrinsicWidth();
                            int vh = Math.round(resource.getIntrinsicHeight() * scale);
                            params.height = vh + iv.getPaddingTop() + iv.getPaddingBottom();
                            iv.setLayoutParams(params);
                            return false;
                        }
                    })
                    .into(iv);

//                        final int finalI = i;
//                        photoView.setOnClickListener(new View.OnClickListener() {
//                            @Override
//                            public void onClick(View view) {
//                                //放大
//                                SkipUtils.toMultiImgShow(mContext, photoList, finalI);
//                            }
//                        });

            photo_ll.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        }
        goodsCommDetailsAdapter.addHeaderView(headerView);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
//            case R.id.setting_push_ll:
//                startActivity(PushActivity.class);
//                break;
        }
    }


    /**
     * 评论图片
     */
    public static List<String> getGoodsEvaluateList(int i) {
        List<String> mList = new ArrayList<>();
        if (i > 0)
            mList.add("http://imgsrc.baidu.com/imgad/pic/item/4bed2e738bd4b31c027523868cd6277f9e2ff846.jpg");
        if (i > 1)
            mList.add("http://www.lagou.com/image1/M00/01/24/CgYXBlTUYpeAaE1IAAHXd642ctI466.jpg");
        if (i > 2)
            mList.add("http://img0.imgtn.bdimg.com/it/u=4104948062,744646040&fm=11&gp=0.jpg");
        if (i > 3)
            mList.add("http://pic63.nipic.com/file/20150319/7487939_201655610000_2.jpg");
        if (i > 4)
            mList.add("http://getimg.jrj.com.cn/images/2014/01/prnewsimg/one_20140124141205613.jpg");
        if (i > 5)
            mList.add("http://pic35.nipic.com/20131107/7487939_020046479000_2.jpg");
        return mList;
    }
}
